package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsModel;
import com.yidu.ruifeng_logistics.entity.LogisticsRole;
import com.yidu.ruifeng_logistics.entity.LogisticsRoleModel;
import com.yidu.ruifeng_logistics.service.LogisticsModelService;
import com.yidu.ruifeng_logistics.service.LogisticsRoleModelService;
import com.yidu.ruifeng_logistics.service.LogisticsRoleService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.LogisticsUserRoleVo;
import com.yidu.ruifeng_logistics.vo.ModulTreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsRole)表控制层
 *
 * @author 张皓超
 * @since 2021-03-09 14:29:08
 */
@Api(description = "瑞丰物流角色接口")
@RestController
@RequestMapping("logisticsRole")
public class LogisticsRoleController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    RedisTemplate redisTemplate ;//redis

    /**
     * 服务对象
     */
    @Resource
    private LogisticsRoleService logisticsRoleService;


    /**
     * 角色模块对象
     */
    @Resource
    private LogisticsRoleModelService logisticsRoleModelService;

    /**
     * 模块
     */
    @Resource
    private LogisticsModelService logisticsModelService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsRole selectOne(Integer id) {
        return this.logisticsRoleService.queryById(id);
    }

    @ApiOperation(value = "数据回显", notes = "返回base")
    @PostMapping("show")
    public BaseMessage show(Integer id) {
        Map<String, Object>map=new HashMap<>();
        LogisticsRole role = logisticsRoleService.queryById(id);//查询该角色信息
        map.put("data1",role);//返回角色信息
        List<LogisticsRoleModel> logisticsRoleModels = logisticsRoleModelService.queryRoleIdReturnRoleModelIdNoIsNull(id);//通过角色表id查询角色不为空的上级模块

        map.put("data2",logisticsRoleModels);//返回角色的模块
        return BaseMessage.success(map);
    }
    @ApiOperation(value = "拿到角色选择框所需的值", notes = "返回BaseMessage")
    @PostMapping("selectBox")
    public BaseMessage selectBox(){
        List<LogisticsRole> logisticsRoles = logisticsRoleService.queryAll(new LogisticsRole());
        return BaseMessage.success(logisticsRoles);
    }

    @ApiOperation(value = "分页显示", notes = "分页返回所有信息")
    @GetMapping("findAll")
    public Map findAll(Integer page , Integer limit){
        PageHelper.startPage(page,limit);
        List<LogisticsRole> logisticsRoles = logisticsRoleService.queryAll(new LogisticsRole());//查询全部返回list
        PageInfo<LogisticsRole> info = new PageInfo<>(logisticsRoles);
        Map<String, Object> map = new HashMap();
        map.put("msg", "查询成功");
        map.put("count", info.getTotal());
        map.put("data", logisticsRoles);
        map.put("code", 0);
        return map;
    }
    @ApiOperation(value = "增加修改", notes = "返回base")
    @PostMapping("addAndUpdate")
    public BaseMessage addUpdate(Integer roleId,String roleName,String roleRemark, String name){
        int rows = logisticsRoleService.addAndUpdate( roleId,roleName, roleRemark,name);//传进事务 service处理
        return rows>0?BaseMessage.success():BaseMessage.fail();
    }

    @ApiOperation(value = "拿到树形组件的数据", notes = "返回BaseMessage")
    @PostMapping("showData")
    public BaseMessage showData(){
        BaseMessage showData = (BaseMessage)redisTemplate.opsForValue().get("showData");//取出redis
        BaseMessage baseMessage=new BaseMessage();
        if (showData==null) {
            List<LogisticsModel> logisticsModels = logisticsModelService.selectParentIsNull();//查询为空的上级模块 数据
            List<ModulTreeNode> modulTreeNodes = new ArrayList<>();//页面需要的list
            for (LogisticsModel model : logisticsModels) {
                ModulTreeNode modulTreeNode = new ModulTreeNode();
                modulTreeNode.setId(model.getModelId());
                modulTreeNode.setTitle(model.getModelName());
                modulTreeNode.setChecked(false);//默认不选中
                modulTreeNode.setSpread(false);
                modulTreeNode.setField("name");
                modulTreeNode.setChildren(selectF(model.getModelId()));//调用递归
                modulTreeNodes.add(modulTreeNode);
            }
            redisTemplate.opsForValue().set("showData",baseMessage.success(modulTreeNodes));//放入redis
            return baseMessage.success(modulTreeNodes);
        }
       return showData;
    }

    /**
     * 树形递归
     *
     * @param modelId
     * @return
     */
    public List<ModulTreeNode> selectF(Integer modelId) {
        List<ModulTreeNode> list = new ArrayList<>();//需要返回
        List<LogisticsModel> modelList = logisticsModelService.selectII(modelId);//查询模块
        for (int i = 0; i < modelList.size(); i++) {
            ModulTreeNode modulTreeNode=new ModulTreeNode();
            modulTreeNode.setId(modelList.get(i).getModelId());
            modulTreeNode.setTitle(modelList.get(i).getModelName());
            modulTreeNode.setChecked(false);
            modulTreeNode.setSpread(false);
            modulTreeNode.setField("name");
            if (modelId != modelList.get(i).getModelParent()) {//如果不等于上级模块不等于传进来的模块id 就直接返回
                return list;
            } else {//否则继续
                modulTreeNode.setChildren(selectF(modelList.get(i).getModelId()));//递归
                list.add(modulTreeNode);
            }
        }

        return list;
    }

}
